<?php
/**
 * 前台登录注册
 * @author lzy <1029460965@qq.com>
 */

namespace app\home\controller;
use app\common\model\User;
use app\common\model\Login as LoginModel;
use think\Controller;
use think\Session;
use think\Log;
use app\common\model\Logs;
use app\common\model\Aes;
use app\common\model\Verify;
class Login extends Controller
{
    /**
     * 登录界面
     */
    public function index(){
        return view();
    }
    /**
     * 用户注册
     */
    public function register(){

        if (request()->isPost()){

            $data=input('post.');
            $userModel=new User();
            if ($data['pwd']!==$data['rpwd']){
                show(0,'密码不一致，请重新输入');
            }
            $data['pwd']=createPwd($data['pwd']);

            unset($data['rpwd']);

            $data['time']=time();
            if ($userModel->add($data)){
                show(1,'提交成功');
            }else{
                show(0,'提交失败');
            }

        }
        return view();
    }

    /**
     * 用户登录
     */
    public function login(){
        if (request()->isPost()){
            $username = input('post.tel');
            $password =input('post.pwd');
            if(!trim($username) || !trim($password)) {
                show(false,'请填写账户、密码');
            }

            $userModel = new User();
            $loginModel = new LoginModel();

            $field = "id,name,passd,valid";
            $where['phone'] = $username;
            $ret = $userModel->getOne($field,$where);
            $login = $loginModel->checkLogin($ret['id']);

            if(!$login){
                show(false,'错误登录次数过多,请'.config('CHECK_OUT_TIME').'分钟后再登录');
            }
            if(!$ret || $ret['valid'] !=1) {
                show(0,'该用户不存在');
            }
            if($ret['passd'] != createPwd($password)) {
                $loginModel->addLogin($ret['id']);
                show(0,'密码错误');
            }

            $loginModel->delLogin($ret['id']);

            $user['id']=$ret['id'];
            $user['name']=$ret['name'];
            $user['type'] = $ret['type'];
            $user['valid'] = $ret['valid'];
            
            session('user', $user);
            
            show(1,'登录成功');
        }
        return view();
    }

    /**
     * 发送验证码
     */
    public function sendemail(){
        if (request()->isAjax()){

            $verifyModel = new Verify();
            $tel = input('post.tel');

            $res = $verifyModel->sendTelCode($tel);
            
            exit(json_encode($res));
        }
    }

    /**
     * 验证信息
     */
    public function getpass(){
        if (request()->isPost()){
            $verifyModel = new Verify();
            $email=input('post.tel');
            $code=input('post.code');
            $res=$verifyModel->Ver($email,$code);

            exit(json_encode($res));
        }
    }

    /**
     * 退出
     * @return [type] [description]
     */
    public function outlogin(){
        session::clear();

        $this->redirect('Login/index');
    }
}